package org.zerocouplage.component.impl.config;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.zerocouplage.component.api.config.IZCTemplateLoader;

public class ZCTemplateLoaderImpl implements IZCTemplateLoader {

	@SuppressWarnings("unused")
	private static ZCTemplateLoaderImpl instance;
	private ZCTemplateConfigImpl templateConfig;

	public ZCTemplateLoaderImpl(Class<?> cl) {
		load(cl);

	}

	public void load(Class<?> cl) {

		String path = cl.getPackage().getName().replaceAll(".", "/");
		String content = null;
		BufferedReader buffer = null;
		String tempateName = cl.getName() + ".zct";
		InputStream templateStream;

		try {

			templateStream = cl.getClassLoader().getResourceAsStream(
					path + "/" + tempateName);
			buffer = new BufferedReader(new InputStreamReader(templateStream));
			while (buffer.ready() == true) {
				content = buffer.readLine();
			}
		} catch (NullPointerException a) {
			System.out
					.println("Error: pointer is null ,create the template in the same package in eclipse ");
		} catch (IOException a) {
			System.out.println("Problem of IO");
		}

		templateConfig.setContent(content);
	}

}
